package com.mm.cloud.common.security.aspect;

import com.mm.cloud.common.core.constant.SecurityConstants;
import com.mm.cloud.common.core.exception.BaseException;
import com.mm.cloud.common.security.helper.LoginUserHelper;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * 内部认证切面
 *
 * @author mr
 * @since 2023-04-20
 */
@Aspect
@Component
public class InnerAuthAspect {

    @Pointcut("@annotation(com.mm.cloud.common.security.annotation.InnerAuth)")
    public void innerAuth() {
    }

    @Around("innerAuth()")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        String source = LoginUserHelper.getRequest().getHeader(SecurityConstants.FROM_SOURCE);
        // 内部请求验证
        if (!StringUtils.equals(SecurityConstants.INNER, source)) {
            throw new BaseException("没有内部访问权限，不允许访问");
        }
        return point.proceed();
    }

}
